{layout name="layout/header" /}
<link href="root/css/plugins/footable/footable.core.css" rel="stylesheet">
<link href="root/css/plugins/iCheck/custom.css" rel="stylesheet">

<link href="root/css/plugins/switchery/switchery.css" rel="stylesheet">
<script src="root/js/plugins/switchery/switchery.js"></script>
<!--<link href="root/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">-->
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5><a href="{:url('vote/index')}">活动列表</a>/{$vote.name}</h5>
                </div>

                <div class="ibox-content">
                    <p>
                        <a type="button" class="btn btn-outline btn-success" href="{:url('add',['vote_id'=>$vote.id])}">新增</a>
                        {if condition="env('ENABLE_BATCH')"}
                        <a class="btn btn-success btn-outline" href="{:url('batch',['vote_id'=>$vote.id])}">批量导入</a>
                        {/if}
                        <a class="btn btn-success btn-outline" onclick="downloadData()" href="#">投票记录下载</a>
                        <a class="btn btn-success btn-outline" href="{:url('downloadCount',['vote_id'=>$vote.id])}">统计下载</a>
                        <!--<button type="button" class="btn btn-outline btn-danger del" pid="0">清空所有数据</button>-->
                        <button type="button" class="btn btn-primary btn-outline refresh">刷新</button>
                        <a type="button" class="btn btn-primary btn-outline" href="{:url('vote/index')}">返回列表</a>
                    </p>
                    <form class="form-inline">
                        <div class="form-group">
                            <select class="form-control " id="order_by">
                                <option value="0">默认排序</option>
<!--                                <option value="real">真实得票数</option>-->
<!--                                <option value="extra">手动得票数</option>-->
                                <option value="total">总票数</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <select class="form-control " id="order">
                                <option value="desc">倒序</option>
                                <option value="asc">正序</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <select class="form-control " id="choose_type">
                                <option value="0">请选择</option>
                                {volist name="$type" id="vo"}
                                <option value="{$vo.id}">{$vo.name}</option>
                                {/volist}
                            </select>
                        </div>
                        <div class="form-group">
                            <input type="text" id="keyword" class="form-control" placeholder="名称/编号" value="{$keyword}">
                        </div>
                        <a class="btn btn-success search">查询</a>
                    </form>
                </div>
                <div class="ibox-content">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>
                                    <input type="checkbox" class="i-checks" name="all" >
                                </th>
                                <th>ID</th>
                                <th>排序</th>
                                <th>编号</th>
                                <th>名称</th>
                                <th>分类</th>
<!--                                <th>真实得票数</th>-->
<!--                                <th>手动得票数</th>-->
                                <th>总票数</th>
                                <th>显示</th>
                                <th>投票</th>
                                <th>备注</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                        {volist name="data.list" id="vo"}
                        <tr pid="{$vo.id}" >
                            <td>
                                <input type="checkbox" class="i-checks" name="ids[]" value="{$vo.id}">
                            </td>
                            <td>{$vo.id}</td>
<!--                            <td><input type="text" class="form-control update_text" data-field="sort" value="{$vo.sort}"></td>-->
<!--                            <td><input type="text" class="form-control update_text" data-field="mid" value="{$vo.mid}"></td>-->
<!--                            <td><input type="text" class="form-control update_text" data-field="name" value="{$vo.name}"></td>-->
                            <td>{$vo.sort}</td>
                            <td>{$vo.mid}</td>
                            <td>{$vo.name}</td>
                            <td>{$vo.type_name}</td>
<!--                            <td>{$vo.real}</td>-->
<!--                            <td><input type="text" class="form-control update_text" data-field="extra" value="{$vo.extra}"></td>-->
                            <td>{$vo.total}</td>
                            <td>
                                <input type="checkbox" class="js-switch update_checkbox" data-field="display" {eq name="$vo.display" value="1"}checked{/eq} />
                            </td>
                            <td>
                                <input type="checkbox" class="js-switch update_checkbox" data-field="enable" {eq name="$vo.enable" value="1"}checked{/eq} />
                            </td>
                            <td>{$vo.remark}</td>
                            <td>{$vo.addtime|date="Y-m-d H:i",###}</td>
                            <td>
                                <a type="button" class="btn btn-info btn-sm " href="{:url('update',['id'=>$vo.id])}">编辑</a>
                                <!--<a type="button" class="btn btn-info btn-sm " href="{:url('attachment',['id'=>$vo.id])}">附件</a>-->
                                <button class="btn btn-danger btn-sm del" pid="{$vo['id']}">删除</button>
                            </td>
                        </tr>
                        {/volist}
                        </tbody>
                        <tfoot>
                        <tr>
                            <td colspan="5">
                                {$data.page}
                            </td>
                        </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<!--新添弹窗-->
<div id="modal-form" class="modal fade" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <div class="row">
                    <div class="col-sm-12 b-r">
                        <h3 class="m-t-none m-b">新增</h3>
                        <form id="form">
                            <div class="form-group">
                                <label>编号</label>
                                <input type="text" placeholder="编号" id="mid" name="mid" class="form-control">
                            </div>
                            <div class="form-group">
                                <label>名称</label>
                                <input type="text" placeholder="名称" id="name" name="name" class="form-control">
                            </div>
                            <div class="form-group">
                                <label>备注</label>
                                <input type="text" placeholder="备注" id="remark" name="remark" class="form-control">
                            </div>
                            <!--<div class="form-group">-->
                                <!--<label>排序</label>-->
                                <!--<input type="text" placeholder="排序" id="sort" name="sort" class="form-control">-->
                            <!--</div>-->
                            {notempty name="$type"}
                            <div class="form-group">
                                <label >分类</label>
                                <select class="form-control m-b" id="type" name="type">
                                    <option value="0">请选择</option>
                                    {volist name="$type" id="vo"}
                                    <option value="{$vo.id}">{$vo.name}</option>
                                    {/volist}
                                </select>
                            </div>
                            {/notempty}
                            <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-2">
                                    <button class="btn btn-primary" id="sub" data-dismiss="modal">提交</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!--新添结束-->
<!--layer-->
<script src="root/js/plugins/layer/layer.min.js"></script>
<!-- iCheck -->
<script src="root/js/plugins/iCheck/icheck.min.js"></script>
<script>
    $('input[name="all"]').on('ifChecked', function(event){
        $('input[name="ids[]"]').iCheck('check')
    });
    $('input[name="all"]').on('ifUnchecked', function(event){
        $('input[name="ids[]"]').iCheck('uncheck')

    });
    function downloadData() {
        var ids = [];
        $('input[name="ids[]"]:checked').each(function (k,v) {
            ids.push($(this).val())
        });
        if(ids.length == 0){
            layer.msg('请至少选择一个');
            return false;
        }
        window.location.href="{:url('downloadDataList')}" + "?ids="+ids;

    }
</script>
<script>
    $(document).ready(function() {
        var vote_id = "{$vote.id}";
        var keyword,type_id,order_by,order;
        type_id = "{$type_id}";
        order_by = "{$order_by}";
        order = "{$order}";
        if(type_id)
            $('#choose_type').val(type_id);
        if(order_by)
            $('#order_by').val(order_by);
        if(order)
            $('#order').val(order);
        checks();

        var elem = document.querySelectorAll('.js-switch');
        elem.forEach(function(i,index){
            var a= "a" +index;
            a = new Switchery(i, {
            color: '#1AB394'
            });
        });

        //搜索
        $('.search').click(function () {
            keyword = $("#keyword").val().trim();
            type_id = $("#choose_type").val();
            order_by= $('#order_by').val();
            order = $('#order').val();

            var url = "{:url('index',['vote_id'=>$vote.id])}"+'/type_id/'+type_id+'/order_by/'+order_by+'/order/'+order+'/keyword/'+keyword;
            window.location.href = url;
        });

        //新添扩展字段
        $('#sub').click(function () {
            var name = $('#name').val().trim();
            var mid = $('#mid').val().trim();
            if(name == '' ){
                layer.msg('名称不能为空!');
                return false;
            }
            if(mid == '' ){
                layer.msg('编号不能为空!');
                return false;
            }
            var remark = $('#remark').val().trim();
            // var sort = $("#sort").val().trim();
            var type = $('#type').val();

            $.ajax({
                url:"{:url('ajaxAdd')}",
                type:"post",
                data:{vote_id:vote_id,name:name,mid:mid,remark:remark,vote_type_id:type},
                dataType:"json",
                success:function (data) {
                    if(data.code == 1){
                        layer.msg(data.msg);
                        setTimeout(function(){
                            window.location.reload();
                        },1500);
                        return false;
                    }else {
                        layer.msg(data.msg);
                        return false;
                    }
                }
            });
        });
        //列表页修改
        $(".update_checkbox").change(function () {
            var field = $(this).attr('data-field');
            var value = $(this).is(':checked')?1:0;
            var id = $(this).parent().parent().attr('pid');
            update(id,field,value);
        });
        //列表页修改
        $(".update_text").blur(function () {
            var field = $(this).attr('data-field');
            var id = $(this).parent().parent().attr('pid');
            var value = $(this).val().trim();
            update(id,field,value);
        });
        function update(id,field,value) {
            $.ajax({
                url:"{:url('ajaxUpdate')}",
                type:'post',
                dataType:'json',
                data:{field:field,value:value,id:id},
                success:function (data) {
                    console.log(data.msg);
                    if(data.code == 0){
                        layer.msg(data.msg);
                    }
                }
            })
        }
        //删除
        $('.del').click(function(){
            var id = $(this).attr('pid');
            var $this = $(this);
            var msg ='确定删除吗？';
            if(id == 0)
                msg = '确定删除该活动所有数据吗'
            layer.confirm(msg, {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.ajax({
                    url:"{:url('ajax_del')}",
                    type:'post',
                    data:{id:id,vote_id:vote_id},
                    dataType:'json',
                    success:function (data) {
                        layer.msg(data.msg);
                        if(data.code ==1){
                            if(id==0){
                                setTimeout(function () {
                                    window.location.reload();
                                },1500)
                            }else {
                                $this.parent().parent().remove();
                            }
                        }
                    }
                })
            }, function(){

            });

        });

    });
</script>
